package stdio.costa.imoca.servlet;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import stdio.costa.imoca.dto.ReceiverListDTO;
import stdio.costa.imoca.model.Receiver;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;

@Controller
@RequestMapping(value = "/receiver")
public class ReceiverController extends BaseController {
	@RequestMapping(value = "/add")
	public String add(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		System.out.println(accessToken);

		String name = request.getParameter("name");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String district = request.getParameter("district");
		String detail = request.getParameter("detail");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);
		param.add("name", name);
		param.add("province", province);
		param.add("city", city);
		param.add("district", district);
		param.add("detail", detail);

		Receiver receiver = service.path("receiver").path("add")
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).post(Receiver.class);

		System.out.println(receiver);

		param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		ReceiverListDTO receiverList = service.path("receiver")
				.path("get_all").queryParams(param)
				.type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(ReceiverListDTO.class);

		List<Receiver> receivers = receiverList.getReceivers();

		result.put("receivers", receivers);

		return "receivers";
	}

	@RequestMapping(value = "/getAllReceivers")
	public String getAllReceivers(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		ReceiverListDTO receiverList = service.path("receiver")
				.path("get_all").queryParams(param)
				.type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(ReceiverListDTO.class);

		List<Receiver> receivers = receiverList.getReceivers();

		result.put("receivers", receivers);

		return "receivers";

	}

	@RequestMapping(value = "/edit")
	public String edit(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		String uuid = request.getParameter("uuid");
		String name = request.getParameter("name");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String district = request.getParameter("district");
		String detail = request.getParameter("detail");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);
		param.add("accessToken", accessToken);
		param.add("name", name);
		param.add("province", province);
		param.add("city", city);
		param.add("district", district);
		param.add("detail", detail);

		Receiver receiver = service.path("receiver").path("edit").path(uuid)
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(Receiver.class);

		System.out.println(receiver);

		param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		ReceiverListDTO receiverList = service.path("receiver")
				.path("getAllReceivers").queryParams(param)
				.type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(ReceiverListDTO.class);

		List<Receiver> receivers = receiverList.getReceivers();

		result.put("receivers", receivers);

		return "receivers";
	}

	@RequestMapping(value = "/delete")
	public String delete(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		String uuid = request.getParameter("uuid");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);
		param.add("uuid", uuid);

		Receiver receiver = service.path("receiver").path("delete").path(uuid)
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(Receiver.class);

		System.out.println(receiver);

		param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		ReceiverListDTO receiverList = service.path("receiver")
				.path("getAllReceivers").queryParams(param)
				.type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(ReceiverListDTO.class);

		List<Receiver> receivers = receiverList.getReceivers();

		result.put("receivers", receivers);

		return "receivers";
	}

	@RequestMapping(value = "/toEditReceiverPage")
	public String toEditReceiverPage(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		String uuid = request.getParameter("uuid");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		Receiver receiver = service.path("receiver").path(uuid)
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(Receiver.class);

		result.put("receiver", receiver);

		return "edit_receiver";
	}

	@RequestMapping(value = "/to_add_weibo_receiver")
	public String add_weibo_to_receiver(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {
		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		String weiUid = request.getParameter("receiver_weibo_uid");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		Receiver receiver = service.path("weibo").path("user").path(weiUid)
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(Receiver.class);

		result.put("receiver", receiver);

		return "add_weibo_to_receiver";
	}

	@RequestMapping(value = "/add_weibo_receiver")
	public String add_weibo_receiver(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {
		String accessToken = (String) request.getSession(false).getAttribute(
				"accessToken");

		String weiboUid = request.getParameter("weiboUid");
		String name = request.getParameter("name");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String district = request.getParameter("district");
		String detail = request.getParameter("detail");

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);
		param.add("weibo_uid", weiboUid);
		param.add("accessToken", accessToken);
		param.add("name", name);
		param.add("province", province);
		param.add("city", city);
		param.add("district", district);
		param.add("detail", detail);

		Receiver receiver = service.path("receiver").path("addWeiboReceiver")
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(Receiver.class);

		System.out.println(receiver);

		param = new MultivaluedMapImpl();
		param.add("accessToken", accessToken);

		ReceiverListDTO receiverList = service.path("receiver")
				.path("get_all").queryParams(param)
				.type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).get(ReceiverListDTO.class);

		List<Receiver> receivers = receiverList.getReceivers();

		result.put("receivers", receivers);

		return "receivers";
	}
}
